//
//  VVHomeOriginalView.swift
//  Weibo
//
//  Created by 王惠平 on 2017/3/31.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

//首页原创微博视图
class VVHomeOriginalView: UIView {
    
    //懒加载控件
    fileprivate lazy var headImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证等级视图
    fileprivate lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //昵称
    fileprivate lazy var screenNameLabel: UILabel = UILabel(fonSize: 14, textColor: UIColor.darkGray, text: "熊猫大侠")
    
    //会员等级图片
    fileprivate lazy var mBrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //时间
    fileprivate lazy var timeLabel:UILabel = UILabel(fonSize: 12, textColor: UIColor.darkGray, text: "刚刚")
    
    //来源
    fileprivate lazy var sourceLabel:UILabel = UILabel(fonSize: 12, textColor: UIColor.darkGray, text: "来自微博")
    
    //微博内容
    fileprivate lazy var contentLabel:UILabel = {
        
       let label = UILabel(fonSize: 13, textColor: UIColor.darkGray, text: "来自地球的火星人")
        
        //多行显示
        label.numberOfLines = 0
        
        return label
        
    }()
    
    //配图
    fileprivate lazy var pictureView: VVHomePictureView = {
       
        let collectionView = VVHomePictureView()
        
        collectionView.backgroundColor = self.backgroundColor
        
        return collectionView
        
    }()
    
    //记录原创微博的底部约束
    var originalButtomCondtraint: Constraint?
    
    //绑定数据
    var statusViewModel: VVStatusViewModel? {
        
        didSet {
            
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                
                headImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            
            //昵称
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            
            //微博内容
            contentLabel.attributedText = statusViewModel?.originalAtttibutedString
            
            //设置认证类型等级图片
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            
            //设置会员等级图片
            mBrankImageView.image = statusViewModel?.mbrankImage
            
            //设置来源数据
            sourceLabel.text = statusViewModel?.sourceContent
            
            //设置时间
            timeLabel.text = statusViewModel?.timeContent
            
            //先卸载之前的原创微博的底部约束
            originalButtomCondtraint?.deactivate()
            
            //判断原创微博是否有图片
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0 {
                
                //表示有原创微博的图片
                pictureView.isHidden = false
                
                //重新创建约束
                self.snp.makeConstraints({ (make) in
                    
                    originalButtomCondtraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                
                //绑定原创微博配图的图片
                pictureView.picUrls = picUrls
            } else {
                
                //表示没有原创微博图片
                pictureView.isHidden = true
                
                //重新创建约束
                self.snp.makeConstraints({ (make) in
                    
                    originalButtomCondtraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
        }
        
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件设置约束
    fileprivate func setupUI() {
        
        backgroundColor = UIColor.white
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mBrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        
        headImageView.snp.makeConstraints { (make) in
            
            make.top.left.equalTo(self).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(headImageView.snp.trailing)
            make.centerY.equalTo(headImageView.snp.bottom)
            
        }
        
        screenNameLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(headImageView)
            make.left.equalTo(headImageView.snp.right).offset(HomeCellMargin)
            
        }
        
        mBrankImageView.snp.makeConstraints { (make) in
            
            make.top.equalTo(screenNameLabel)
            make.left.equalTo(screenNameLabel.snp.right).offset(HomeCellMargin)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            
            make.left.equalTo(headImageView.snp.right).offset(HomeCellMargin)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            
            make.left.equalTo(timeLabel.snp.right).offset(HomeCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(headImageView.snp.bottom).offset(HomeCellMargin)
            make.left.equalTo(headImageView)
            make.right.equalTo(self).offset(-HomeCellMargin)
        }
        
        pictureView.snp.makeConstraints { (make) in
            
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.left.equalTo(contentLabel)
            
            //默认值大小
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //关键约束
        self.snp.makeConstraints { (make) in
            
           originalButtomCondtraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
    
    }
    
}

